package com.trailwalker.traceability.activity;

import com.trailwalker.JavaUtils.string.StringUtils;
import com.trailwalker.javafx.core.activity.Activity;
import com.trailwalker.javafx.utils.image.ImageUtils;
import com.trailwalker.javafx.utils.stage.StageUtils;
import com.trailwalker.javafx.utils.storage.PreferencesManager;
import com.trailwalker.javafx.utils.style.AdaptUtils;
import com.trailwalker.javafx.utils.style.StyleUtils;
import com.trailwalker.javafx.utils.system.ApplicationUtils;
import com.trailwalker.traceability.TraceabilitySystemApplication;
import com.trailwalker.traceability.entitiy.enum_entity.ProcessType;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;

/**
 * @author Trailwalker
 * @create 2021-11-11 11:25
 */
public abstract class BaseActivity extends Activity {

    protected Activity mContext;
    protected AdaptUtils mAdaptUtils;
    protected TraceabilitySystemApplication mApplication;

    @Override
    public void onCreate() {
        super.onCreate();
        initSuperLoad();
        initSuperView();
        initSuperData();
        initSuperListener();
        onSuperReady();
        afterSuperCreate();
    }

    private void onSuperReady() {

    }

    private void initSuperListener() {

    }

    private void initSuperData() {

    }

    private void initSuperView() {
        AnchorPane apRoot = new AnchorPane();
        setContentView(apRoot);

        BorderPane bpRoot = new BorderPane();
        apRoot.getChildren().add(bpRoot);
        mAdaptUtils.setMatchParentAnchorPane(bpRoot);

        Node titleView = initTitleView();
        bpRoot.setTop(titleView);
        Node contentView = initContentView();
        bpRoot.setCenter(contentView);
    }

    private Node initTitleView() {
        HBox hbTitle = new HBox();
        setTitleView(getApplication().getStage(), hbTitle);
        mAdaptUtils.setScaleHeight(hbTitle, 50);
        hbTitle.getStyleClass().add("title");
        mAdaptUtils.setScalePaddingLeftAndRight(hbTitle, 20);
        hbTitle.setAlignment(Pos.CENTER);
        hbTitle.setSpacing(mAdaptUtils.getScale(30));

        Label lblTitle = new Label("益华扫码流程追溯系统");
        hbTitle.getChildren().add(lblTitle);
        mAdaptUtils.setScaleTextSize(lblTitle, 20);
        StyleUtils.setTextColor(lblTitle, "#ffffff");

        boolean isLogin = mApplication.getGlobalData().getBoolean("isLogin", false);
        if (isLogin) {
            String account = mApplication.getGlobalData().getString("account", "");
            if (StringUtils.isValid(account)) {
                Label lblAccount = new Label();
                hbTitle.getChildren().add(lblAccount);
                mAdaptUtils.setScaleTextSize(lblAccount, 20);
                StyleUtils.setTextColor(lblAccount, "#ffffff");
                lblAccount.setText("你好，" + account);
            }
        }

        Region hGrowRegion = new Region();
        hbTitle.getChildren().add(hGrowRegion);
        HBox.setHgrow(hGrowRegion, Priority.ALWAYS);

        if (isLogin) {
            Label lblReload = new Label();
            hbTitle.getChildren().add(lblReload);
            lblReload.setText("首页");
            mAdaptUtils.setScaleTextSize(lblReload, 20);
            StyleUtils.setTextColor(lblReload, "#ffffff");
            lblReload.setCursor(Cursor.HAND);
            lblReload.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    if (event.getButton().equals(MouseButton.PRIMARY)) {
                        PreferencesManager preferencesManager = PreferencesManager.getInstance();
                        preferencesManager.putInt("processTypeIndex", ProcessType.UNKNOWN.getIndex());
                        mApplication.reload();
                    }
                }
            });

            Label lblExit = new Label();
            hbTitle.getChildren().add(lblExit);
            lblExit.setText("退出登录");
            mAdaptUtils.setScaleTextSize(lblExit, 20);
            StyleUtils.setTextColor(lblExit, "#ffffff");
            lblExit.setCursor(Cursor.HAND);
            lblExit.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    if (event.getButton().equals(MouseButton.PRIMARY)) {
                        PreferencesManager preferencesManager = PreferencesManager.getInstance();
                        preferencesManager.putInt("processTypeIndex", ProcessType.UNKNOWN.getIndex());
                        preferencesManager.putString("account", "");
                        preferencesManager.putString("password", "");
                        mApplication.reload();
                    }
                }
            });
        }

        ImageView ivIconified = ImageUtils.getImageView(this, 20, "iconified_normal.png", "iconified_press.png");
        hbTitle.getChildren().add(ivIconified);
        ivIconified.setCursor(Cursor.HAND);
        ivIconified.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                if (event.getButton().equals(MouseButton.PRIMARY)) {
                    StageUtils.setIconified(mApplication.getStage());
                }
            }
        });

        ImageView ivClose = ImageUtils.getImageView(this, 20, "close_normal.png", "close_press.png");
        hbTitle.getChildren().add(ivClose);
        ivClose.setCursor(Cursor.HAND);
        ivClose.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                if (event.getButton().equals(MouseButton.PRIMARY)) {
                    ApplicationUtils.exit();
                }
            }
        });

        return hbTitle;
    }

    private void initSuperLoad() {
        mContext = this;
        mAdaptUtils = AdaptUtils.getInstance();
        mApplication = (TraceabilitySystemApplication) getApplication();
    }

    protected abstract void afterSuperCreate();

    protected abstract Node initContentView();

}
